package com.sc.search.service.impl;

import com.sc.common.pojo.CommonResult;
import com.sc.common.pojo.SearchItem;
import com.sc.search.ItemMapper.ItemMapper;
import com.sc.search.service.ItemService;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Created by sc on 2017/10/24.
 */
@Service
public class ItemServiceImpl implements ItemService{

    @Autowired
    private SolrServer solrServer;
    @Autowired
    private ItemMapper itemMapper;

    public CommonResult importDataToSolr() throws IOException, SolrServerException {
        List<SearchItem> itemList = itemMapper.getItemList();
        for(SearchItem item:itemList){
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id",item.getId()) ;
            document.addField("item_title",item.getTitle());
            document.addField("item_category_name",item.getItem_category_name());
            document.addField("item_image",item.getImage());
            document.addField("item_desc",item.getItem_desc());
            document.addField("item_sell_point",item.getSell_point());
            document.addField("item_price",item.getPrice());
            solrServer.add(document);
        }
        solrServer.commit();
        return CommonResult.ok();
    }
}
